package com.application.fabric.users.controller;

import com.application.fabric.fabriccontrol.fabricinit.register.RegisterUser;
import com.application.fabric.users.config.Dealmsg;
import com.application.fabric.users.config.encryptAl.EncrypAl;
import com.application.fabric.users.config.encryptAl.PictureMain;
import com.application.fabric.users.config.encryptAl.TokenVer;
import com.application.fabric.users.entity.LoginSuccessmsg;
import com.application.fabric.users.entity.PictureEntity;
import com.application.fabric.users.entity.Returnmsg;
import com.application.fabric.users.entity.Usermessage;
import com.application.fabric.users.service.SeLoginusers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.util.*;

@RestController
@RequestMapping("/Users")
public class UsersControl {
    @Autowired
    private SeLoginusers seusrmsg;


    /*信息返回函数*/
    @Autowired
    private Dealmsg dealmsg;

    @Resource
    private TokenVer tokencol;

    //    得到图片验证码信息
    @Autowired
    private PictureMain picmain;
    @Autowired
    private PictureEntity picentity;
    /*登陆接口*/
//    @CrossOrigin
    @PostMapping("Login")
    public Map<String,Object> LoginUsers(@RequestBody Usermessage usermessage){
//        验证码检测
        if (usermessage.getVercode().equals(picentity.getCode())){
            System.out.println("验证码通过");
            /*调用查询接口测试 手机号以及密码验证*/
            LoginSuccessmsg testmsg = seusrmsg.commonlogin(usermessage);
            //    返回信息实体类 是对返回信息进行过滤
            Returnmsg returnmsg = new Returnmsg();
            if (testmsg.getPublickey() != null && testmsg.getPhonenum() != null){
//            token设置返回
                String msgmain = tokencol.geneJsonWebToken(testmsg.getPublickey(),testmsg.getPhonenum(),testmsg.getUserid());
                // map中为返回数据 放在data中返回 信息放在token中
                Map<String,Object> map = new HashMap<>();
                map.put("token",msgmain);
                map.put("userid",testmsg.getUserid());
                returnmsg.setData(map);
                return dealmsg.successmsg(returnmsg);
            }
            else {
                return dealmsg.loginfailmsg(returnmsg);
            }
        }else if (usermessage.getVercode() == null){
            System.out.println("验证码为空");
            return dealmsg.picnull();
        }
        else {
            System.out.println("打印获取验证码"+ picentity.getCode());
            return dealmsg.picfail();
        }

    }

    @Autowired
    private EncrypAl encrypAl;
    @Autowired
    private RegisterUser registerUser;

    @CrossOrigin
    @PostMapping("Register")
    public Map<String,Object> RegisterUsers(@RequestBody Usermessage usermessage) throws Exception  {
        /*
        * 先进行重名验证 如果不重名则进行注册 否则返回注册失败 用户名已经存在
        * */
        /*不重名为1 重名为0*/
        /*调用增加用户接口接口测试*/
        int flag = seusrmsg.registerve(usermessage);
        Dealmsg msg =new Dealmsg();
        if (flag == 1){
            //返回成功信息
            return msg.registersuccessmsg();
        }
        /*重名*/
        else {
            return msg.registerfailmsg();
        }

    }

    /*wx - login
    * */
    @CrossOrigin
    @GetMapping("wxLogin")
    public Map<String,Object> wxLoginUsers(@RequestBody Usermessage usermessage) throws Exception {
        /*微信信息验证 主要逻辑为查询手机号是否存在 不存在则注册 存在则显示登陆成功并且返回提示信息*/
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","请求成功");
        map.put("verify",seusrmsg.wxlogin(usermessage));
        return map;
    }

    @GetMapping
    public Map<String,Object> GetEvidenceData(){
        return null;
    }

    @GetMapping("img")
    public ResponseEntity getimg() throws Exception{
        // 将BufferedImage对象转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(picmain.getmsg(), "png", baos);
        baos.flush();
        byte[] imageBytes = baos.toByteArray();
        baos.close();

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

//        picentity.setCode();
        ResponseEntity r = new ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
        picentity.setImg(r);
        // 返回图片数据
        picentity.setCode("");
        picentity.setCode(picmain.getcode());
        picmain.setcode();
        return r;
    }
}

